package com.medical.admin.sys.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medical.admin.sys.common.dto.TitleDto;
import com.medical.admin.sys.service.TitleService;
import com.qf.medical.response.ResponseCode;
import com.qf.medical.response.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/*
 * @Date : 2024/4/28 上午10:16
 * @Created by Lang
 * @ProjectName : medical-appointment-registration
 */
@Api(tags = "职称管理文档")
@RestController
@RequestMapping("/title")
public class TitleController {

    @Resource
    private TitleService titleService;

    @ApiOperation(value = "添加职称")
    @PostMapping("/add")
    public ResponseEntity<String> add(@RequestBody @Valid TitleDto titleDto) {
        Integer add = titleService.add(titleDto);
        if (add > 0) {
            return ResponseEntity.success(ResponseCode.TITLE_ADD_SUCCESS.getMsg());
        }
        return ResponseEntity.success(ResponseCode.TITLE_ADD_ERROR.getMsg());
    }

    @ApiOperation(value = "修改职称")
    @PutMapping("/mod")
    public ResponseEntity<String> mod(@RequestBody @Valid TitleDto titleDto) {
        Integer mod = titleService.mod(titleDto);
        if (mod > 0) {
            return ResponseEntity.success(ResponseCode.TITLE_MOD_SUCCESS.getMsg());
        }
        return ResponseEntity.success(ResponseCode.TITLE_MOD_ERROR.getMsg());
    }

    @ApiOperation(value = "修改职称状态")
    @PutMapping("/modStatus")
    public ResponseEntity<String> del(@RequestParam("titleId")Integer delId,@RequestParam("titleStatus")Integer titleStatus){
        Integer i = titleService.modStatus(delId, titleStatus);
        if (i > 0) {
            return ResponseEntity.success(ResponseCode.TITLE_MOD_STATUS_SUCCESS.getMsg());
        }
        return ResponseEntity.success(ResponseCode.TITLE_MOD_STATUS_ERROR.getMsg());
    }

    @ApiOperation(value = "查询职称")
    @GetMapping("/list")
    public ResponseEntity<Page<TitleDto>> list(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize, String titleName){
        return ResponseEntity.success(titleService.list(pageNum,pageSize,titleName));
    }

    @ApiOperation(value = "删除职称")
    @PutMapping("/del")
    public ResponseEntity<String> del(@RequestParam("titleId")Integer delId){
        Integer del = titleService.del(delId);
        if (del > 0) {
            return ResponseEntity.success(ResponseCode.TITLE_DEL_SUCCESS.getMsg());
        }
        return ResponseEntity.success(ResponseCode.TITLE_DEL_ERROR.getMsg());
    }

    @ApiOperation(value = "批量删除职称")
    @PutMapping("/delBatch")
    public ResponseEntity<String> delBatch(@RequestBody List<Integer> delIds){
        Integer res = titleService.delBatch(delIds);
        if (res > 0) {
            return ResponseEntity.success(ResponseCode.TITLE_DEL_BATCH_SUCCESS.getMsg());
        }
        return ResponseEntity.success(ResponseCode.TITLE_DEL_BATCH_ERROR.getMsg());
    }

    @ApiOperation(value = "根据id查询职称")
    @GetMapping("/find")
    public ResponseEntity<TitleDto> find(@RequestParam("titleId")Integer titleId){
        TitleDto titleDto = titleService.selectById(titleId);
        if (ObjectUtil.isNotEmpty(titleDto)) {
            return ResponseEntity.success(titleDto);
        }
        return ResponseEntity.error(ResponseCode.TITLE_FIND_ERROR);
    }
}
